<?php
/**
 *  Bootstrap
 *
 *  @author $Author: jinsonxu@gmail.com $:
 *  $Date: 2014-11-12 09:54:36 +0000 (Wed, 12 Nov 2014) $:
 *  $Id: bootstrap.php 11 2014-11-12 09:54:36Z jinsonxu@gmail.com $:
 *  @version $Rev: 11 $:
 *  @package geobat.application
 */
use org\crawlgenie\application\path\Path;

include(__DIR__ . DIRECTORY_SEPARATOR . 'conf' . DIRECTORY_SEPARATOR . 'CGConfig.php');
include(__DIR__ . DIRECTORY_SEPARATOR . 'conf' . DIRECTORY_SEPARATOR . 'CGConstants.php');
include(CG_BASE_PATH . DS . CGConfig::$libDirName . DS . 'CGException.php');
include(CG_LOGGER_INCLUDE_PATH . DS . 'Logger.php');
include(CG_LIB_PATH . DS . 'simple_html_dom.php');

include('../../libraries/krumo/class.krumo.php');


date_default_timezone_set(CGConfig::$timezone);

//note that if the logger properties file is not found, any other function that
//makes use of Logger will not be able to complete execution.
Logger::configure(CG_BASE_PATH . DS . 'conf' . DS . 'log.config.properties');

spl_autoload_register('cgAutoload');



/**
 * Remember that autoload is the last ditch after php has failed to locate the class even after
 * looking at all registered include paths.
 *
 * first, check for a namespaced class by seeing whether \ character exists after the first character
 * if true, load it up according to the path hierarchy in src. if false, check for class existence
 * in main library directory
 */
function cgAutoload($className) {

    if(stripos(substr($className, 1), '\\') !== false) {
        $expandedPath = (DS != '\\') ? str_replace('\\', DS, substr($className, 0)) . '.php' : $className . '.php';
        $classPath = CG_SOURCES_DIR . DS . $expandedPath;

        Logger::getLogger(__FUNCTION__)->debug('Namespaced file detected ('.$className.'), including at '. $classPath);

        if(file_exists($classPath)) {
            include($classPath);

            if(!class_exists($className, false) && !interface_exists($className, false)) {
                throw new CGException('Included file at ' . $classPath . ' does not contain class or interface ' . $className);
            }
        } else {
            throw new CGException('No file exists for inclusion at '. $classPath
                .' for namespaced class: ' . $className);
        }

    } else {
        //look for the class in the main library folder
        $classPath = dirname(__FILE__) . DS . CGConfig::$libDirName . DS . $className . '.php';
        Logger::getLogger(__FUNCTION__)->debug('Unnamespaced file detected, looking in ' . $classPath);

        if(file_exists($classPath)) {
           include($classPath);
            if(!class_exists($className, false)) {
                throw new CGException('Included file at ' . $classPath . ' does not contain class ' . $className);
            }

        } else {
            throw new CGException('No file exists for inclusion at '. $classPath .' for class: ' . $className);
        }
    }
}

?>